package com.augurit.common.sysconfig.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.dao.SysConfigDao;
import com.augurit.common.entity.SysConfig;
import com.augurit.common.sysconfig.convert.SysConfigConvertor;
import com.augurit.common.sysconfig.service.ISysConfigService;
import com.augurit.common.sysconfig.web.form.SysConfigForm;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;

@Service
@Transactional
public class SysConfigServiceImpl implements ISysConfigService{

	@Autowired
	private SysConfigDao sysConfigDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public SysConfigForm get(Long id) {
		SysConfig entity = sysConfigDao.get(id);
		return SysConfigConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		sysConfigDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(SysConfigForm... forms) {
		if(forms != null)
			for(SysConfigForm form : forms)
				this.save(form);
	}

	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(SysConfigForm form){
		
		if(form != null){
			SysConfig entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = sysConfigDao.get(form.getId());
			}else{
				entity = new SysConfig();
			}
			
			//属性值转换
			SysConfigConvertor.convertFormToVo(form, entity);
			
			//保存
			sysConfigDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<SysConfigForm> search(Page<SysConfigForm> page, SysConfigForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysConfig ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			//TODO
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		sysConfigDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysConfigForm> list = SysConfigConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 分页获取所有对象
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<SysConfigForm> getAll(Page<SysConfigForm> page){
		// 建立新的分页对象
		Page<SysConfig> pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		String hql = "from SysConfig ps order by ps.key";
		sysConfigDao.findPage(pg, hql);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysConfigForm> list = SysConfigConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 获取所有对象
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysConfigForm> getAll(){
		return SysConfigConvertor.convertVoListToFormList(sysConfigDao.getAll());
	}
	
	/**
	 * 获取系统首页编号
	 * @return
	 */
	@Transactional(readOnly = true)
	public String getSystemHomepageCode(){
		SysConfig entity = sysConfigDao.findUniqueBy("key", WebContextUtils.SYS_CONFIG_HOMEPAGE);
		if(entity != null)
			return entity.getValue();
		else
			return null;
	}

	/**
	 * 更新指定配置项的值
	 * @param key
	 * @param value
	 */
	public void updateItem(String key, String value){
		
		if(key != null && key.trim().length() > 0
				&& value != null && value.trim().length() > 0){
			
			SysConfig entity = sysConfigDao.findUniqueBy("key", key);
			
			if(entity != null){
				entity.setValue(value);
				sysConfigDao.save(entity);
			}
		}
	}
	
	public Page<SysConfigForm> search(Page<SysConfigForm> page, List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}
}
